package org.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;

import java.io.File;
import java.io.IOException;
import java.util.List;

class Main {
    public static void main(String[] args) throws IOException {
        xpathMethod();
    }

    public static void xpathMethod() throws IOException {
        String path = Main.class.getClassLoader().getResource("student.xml").getPath();
        Document document = Jsoup.parse(new File(path), "utf-8");

        // ✅ 新版推荐使用 JXDocument.create() 静态方法创建实例
        JXDocument jxDocument = JXDocument.create(document);

        // 示例 XPath 查询
        List<JXNode> jxNodes = jxDocument.selN("//users/user");
        for (JXNode jxNode : jxNodes) {
            System.out.println(jxNode);
        }
        System.out.println("--------------------");

        List<JXNode> jxNodes2 = jxDocument.selN("//users/user/name");
        for (JXNode jxNode : jxNodes2) {
            System.out.println(jxNode);
        }
        System.out.println("--------------------");

        List<JXNode> jxNodes3 = jxDocument.selN("//users/user[@id]");
        for (JXNode jxNode : jxNodes3) {
            System.out.println(jxNode);
        }
        System.out.println("--------------------");

        List<JXNode> jxNodes4 = jxDocument.selN("//users/user/name[@id='itcast']");
        for (JXNode jxNode : jxNodes4) {
            System.out.println(jxNode);
        }
    }

    public static void jsoupMethod(String[] args) throws IOException {
        String path = Main.class.getClassLoader().getResource("student.xml").getPath();
        Document document = Jsoup.parse(new File(path), "utf-8");
        Elements elements = document.getElementsByTag("name");
        System.out.println(elements.size());
        Element element = elements.get(0);
        String name = element.text();
        System.out.println(name);
    }
}